﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAIN.MVC.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List()
        {
            // GUID 3A8DF15F-21E7-40D8-BE3C-5E785A394086

            List<BE.WUC.Menu.MenuItemBE> result = new List<BE.WUC.Menu.MenuItemBE>();
            BE.Security.Login.UsuarioBE usuario = new Membership().CurrentUser;
            result = BC.WUC.Menu.MenuBC.GetMenu(usuario);


            SAIN.MVC.Models.LeftSideMenu menu = new Models.LeftSideMenu();

            var modulos = (from i in result select new { i.iModuleId, i.sModuleName }).Distinct();

            foreach (var modulo in modulos)
            {
                SAIN.MVC.Models.Modulo moduloItem = new Models.Modulo()
                {
                    bActive = true,
                    sModuleName = modulo.sModuleName,
                    iModuleId = modulo.iModuleId,
                    sDescription =string.Empty
                };
                foreach (var menuItem in (from i in result
                                          where i.iModuleId == moduloItem.iModuleId
                                          select i))
                {
                    moduloItem.MenuItems.Add(
                        new Models.SiteMenuItem()
                        {
                            iModuleId = moduloItem.iModuleId,
                            iMenuItemId =  menuItem.iMenuItemId,
                            sMenuName = menuItem.sMenuName,
                            sMenuDescription = menuItem.sMenuDescription,
                            bActive = true,
                            sPage = menuItem.sPage
                        });
                }
                menu.Modulos.Add(moduloItem);
            }
            return View(menu);

        }

    }
}
